package com.seed.bpmn.rest;

import com.seed.bpmn.engine.IBPMNHistoryService;
import com.seed.bpmn.engine.common.util.ResultData;
import com.seed.bpmn.engine.dto.ProcessInstanceDTO;
import com.seed.bpmn.engine.dto.TaskDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Api(description = "历史级别流程数据操作接口")
@Controller
@RequestMapping("/bpmn/histroy")
public class HistroyController {

    @Resource(name = "bpmnHistoryService")
    private IBPMNHistoryService historyService;

    @ApiOperation(value = "通过流程实例ID获取历史级别流程实例")
    @RequestMapping(value = "/process-instance/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JSON getProcessInstance(@PathVariable String id) {
        ResultData data = null;
        try {
            ProcessInstanceDTO p = historyService.getProcessInstance(id);

            data = ResultData.success("操作成功！", p);
        } catch (Exception e) {
            data = ResultData.failure("操作失败！");
        }
        return data.toJson();
    }

    @ApiOperation(value = "获取办结状态流程实例")
    @RequestMapping(value = "/process-instance/finished/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JSON getFinishProcessInstance(@PathVariable String id) {
        ResultData data = null;
        try {
            ProcessInstanceDTO p = historyService.getFinishProcessInstance(id);
            data = ResultData.success("操作成功！", p);
        } catch (Exception e) {
            data = ResultData.failure("操作失败！");
        }
        return data.toJson();
    }

    @ApiOperation(value = "通过任务id查询历史级别任务")
    @RequestMapping(value = "/task/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JSON getTask(@PathVariable String id) {
        ResultData data = null;
        try {
            TaskDTO t = historyService.getTask(id);
            data = ResultData.success("操作成功！", t);
        } catch (Exception e) {
            data = ResultData.failure("操作失败！");
        }
        return data.toJson();
    }

    @ApiOperation(value = "通过流程实例Id查询历史级别任务")
    @RequestMapping(value = "/task/process-instance-id/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JSON findTaskByProcessInstanceId(@PathVariable String id) {
        ResultData data = null;
        try {
            List<TaskDTO> list = historyService.findTaskByProcessInstanceId(id);
            data = ResultData.success("操作成功！", list);
        } catch (Exception e) {
            data = ResultData.failure("操作失败！");
        }
        return data.toJson();
    }
}
